package com.cenxi.controller;

import com.cenxi.common.aspect.annotation.Permissions;
import com.cenxi.common.constants.Constants;
import com.cenxi.common.dto.req.AccountReqDTO;
import com.cenxi.common.dto.resp.AccountRespDTO;
import com.cenxi.common.dto.resp.LoginAccountRespDTO;
import com.cenxi.common.util.req.PageReq;
import com.cenxi.common.util.req.Req;
import com.cenxi.common.util.resp.PageResp;
import com.cenxi.common.util.resp.Resp;
import com.cenxi.dao.domain.Menu;
import com.cenxi.dao.service.MenuService;
import com.cenxi.manage.ManageAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author cenxi
 * @Date 2023/8/1 11:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/account")
@Slf4j
@Api(tags = {"账户管理"})
public class AccountController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private ManageAccountService manageAccountService;

    @Permissions("comercial:merchantlist:list")
    @GetMapping("/beat/{beatStr}")
    public Object beat(@PathVariable String beatStr) {
        log.info("数据===> {}", beatStr);
        List<Menu> list = menuService.list();
        log.info("路由: {}", list);
        return "没问题=====>"+ beatStr;
    }

    @PostMapping("/page")
    @ApiOperation(value = "账户列表")
    public PageResp<AccountRespDTO> page(@RequestBody PageReq<AccountReqDTO> req) {
        return manageAccountService.page(req);
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建账户")
    public Resp<Boolean> create(@RequestBody Req<AccountReqDTO> req) {
        return manageAccountService.create(req);
    }

    @PostMapping("/finById")
    @ApiOperation(value = "根据id查询平台账户信息")
    public Resp<AccountRespDTO> findById(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageAccountService.findById(req.getData());
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改账户")
    public Resp<Boolean> edit(@RequestBody Req<AccountReqDTO> req) {
        return manageAccountService.edit(req);
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用账户")
    public Resp<Boolean> enable(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageAccountService.enable(req.getData(), Constants.ENABLE);
    }

    @PostMapping("/disable")
    @ApiOperation(value = "禁用账户")
    public Resp<Boolean> disable(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageAccountService.disable(req.getData(), Constants.DISABLE);
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "根据id删除平台账户信息")
    public Resp<Boolean> deleteById(@RequestBody Req<String> req) {
        if (Objects.isNull(req.getData())) {
            return Resp.error("参数错误");
        }
        return manageAccountService.deleteById(req.getData());
    }

    @PostMapping("/info")
    @ApiOperation(value = "获取登录账户信息")
    public Resp<LoginAccountRespDTO> getLoginAccount(@RequestBody Req<Void> req) {
        return manageAccountService.getLoginAccount();
    }
}
